package com.example.shuiyin.service.impl;

import com.example.shuiyin.enums.MediaType;
import com.example.shuiyin.exception.AppException;
import com.example.shuiyin.exception.ErrorCode;
import com.example.shuiyin.processors.MediaProcessor;
import com.example.shuiyin.processors.ProcessorFactory;
import com.example.shuiyin.service.MediaProcessorService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class MediaProcessorServiceImpl implements MediaProcessorService {

    private final ProcessorFactory processorFactory;

    @Override
    public String process(String sourceFilePath, MediaType mediaType) {
        if (sourceFilePath == null || sourceFilePath.isEmpty()) {
            throw new AppException("源文件路径不能为空", ErrorCode.INVALID_PARAMETER);
        }
        
        if (mediaType == null) {
            throw new AppException("无效的媒体类型", ErrorCode.INVALID_PARAMETER);
        }
        
        MediaProcessor processor = processorFactory.getProcessor(mediaType);
        if (processor == null) {
            throw new AppException("不支持的媒体类型", ErrorCode.INVALID_PARAMETER);
        }
        
        try {
            return processor.process(sourceFilePath);
        } catch (Exception e) {
            log.error("处理媒体文件失败: {}", e.getMessage(), e);
            throw new AppException("不支持的媒体类型", ErrorCode.INVALID_PARAMETER);
        }
    }
} 